package com.fc.test.service;

import java.util.List;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.MD5Util;
import com.fc.test.util.StringUtils;
import com.mysql.fabric.xmlrpc.base.Member;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service
* @Title: TAbbRenzhengService.java 
* @Package com.fc.test.entity 
* @author fuce
* @email 87766867@qq.com
* @date 2019-08-20 18:33:53  
 */
@Service
public class TAbbRenzhengService implements BaseService<TAbbRenzheng, TAbbRenzhengExample>{
	@Autowired
	private TAbbRenzhengMapper tAbbRenzhengMapper;
	//生成的用户dao
	@Autowired
	private TsysUserMapper tsysUserMapper;
	//生成的用户dao
	@Autowired
	private SysUserService sysUserService;
	//生成的角色用户dao
	@Autowired
	private TSysRoleUserMapper tSysRoleUserMapper;
	@Autowired
	private TAbbMemberMapper tAbbMemberMapper;
	@Autowired
	private TAbbGoodsMapper tAbbGoodsMapper;
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<TAbbRenzheng> list(Tablepar tablepar,String name,String status){
	        TAbbRenzhengExample testExample=new TAbbRenzhengExample();
	        testExample.setOrderByClause("id ASC");
		 TAbbRenzhengExample.Criteria criteria=testExample.createCriteria();
	        if(name!=null&&!"".equals(name)){
				criteria.andShopnameLike("%"+name+"%");
	        }
		 if(status!=null&&!"".equals(status)&&!status.equals("-1")){
			 criteria.andStatusEqualTo(Integer.parseInt(status));
		 }
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TAbbRenzheng> list= tAbbRenzhengMapper.selectByExample(testExample);
	        PageInfo<TAbbRenzheng> pageInfo = new PageInfo<TAbbRenzheng>(list);
	        return  pageInfo;
	 }
	public PageInfo<TAbbRenzheng> sjlist(Tablepar tablepar, String searchTxt) {
		TsysUser user= (TsysUser) SecurityUtils.getSubject().getPrincipal();
		TAbbRenzhengExample testExample=new TAbbRenzhengExample();
		testExample.createCriteria().andLoginusernameEqualTo(user.getUsername());
		List<TAbbRenzheng> list= tAbbRenzhengMapper.selectByExample(testExample);
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		PageInfo<TAbbRenzheng> pageInfo = new PageInfo<TAbbRenzheng>(list);
		return  pageInfo;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		TAbbRenzhengExample example=new TAbbRenzhengExample();
		example.createCriteria().andIdIn(lista);
		return tAbbRenzhengMapper.deleteByExample(example);
	}


	@Override
	public TAbbRenzheng selectByPrimaryKey(String id) {

		return tAbbRenzhengMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKey(TAbbRenzheng renzheng){
	 	return tAbbRenzhengMapper.updateByPrimaryKey(renzheng);
	}

	@Override
	public int updateByPrimaryKeySelective(TAbbRenzheng record) {
		TAbbMember member=new TAbbMember();
		if (record.getStatus()==1){
			member.setId(record.getUserid());
			member.setIsshop(1);
			tAbbMemberMapper.updateByPrimaryKeySelective(member);
		}
		return tAbbRenzhengMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TAbbRenzheng record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tAbbRenzhengMapper.insertSelective(record);
	}










	@Override
	public int updateByExampleSelective(TAbbRenzheng record, TAbbRenzhengExample example) {

		return tAbbRenzhengMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TAbbRenzheng record, TAbbRenzhengExample example) {

		return tAbbRenzhengMapper.updateByExample(record, example);
	}

	@Override
	public List<TAbbRenzheng> selectByExample(TAbbRenzhengExample example) {

		return tAbbRenzhengMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TAbbRenzhengExample example) {

		return tAbbRenzhengMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TAbbRenzhengExample example) {

		return tAbbRenzhengMapper.deleteByExample(example);
	}

    @Transactional
    public int zhadd(TAbbRenzheng record) {
		TsysUser user=new TsysUser();
		String userid=SnowflakeIdWorker.getUUID();
		user.setId(userid);

		//设置为商家角色
		TSysRoleUser roleUser=new TSysRoleUser(SnowflakeIdWorker.getUUID(), userid,"613690451936935936");
		tSysRoleUserMapper.insertSelective(roleUser);


		//密码加密
		user.setPassword(MD5Util.encode(record.getLoginpassword()));
		user.setUsername(record.getLoginusername());

		//商家认证表里加上保存用户名密码
		tAbbRenzhengMapper.updateByPrimaryKeySelective(record);
		return tsysUserMapper.insertSelective(user);
    }
	@Transactional
	public int zhedit(TAbbRenzheng record) {
		TAbbRenzheng oldrecord=tAbbRenzhengMapper.selectByPrimaryKey(record.getId());

		//更新user表里的账号密码
		TsysUserExample tsysUserExample=new TsysUserExample();
		tsysUserExample.createCriteria().andUsernameEqualTo(oldrecord.getLoginusername());
		List<TsysUser> list=sysUserService.selectByExample(tsysUserExample);
		TsysUser user=list.get(0);
		user.setUsername(record.getLoginusername());
		//密码加密
		user.setPassword(MD5Util.encode(record.getLoginpassword()));
		tsysUserMapper.updateByPrimaryKeySelective(user);

		//更新商家认证表里的账号密码
		return tAbbRenzhengMapper.updateByPrimaryKeySelective(record);
	}
//禁用
@Transactional
	public int jyedit(TAbbRenzheng record) {
		TAbbRenzheng oldrecord=tAbbRenzhengMapper.selectByPrimaryKey(record.getId());
		//更新user表里的的禁用状态
		TsysUserExample tsysUserExample=new TsysUserExample();
		tsysUserExample.createCriteria().andUsernameEqualTo(oldrecord.getLoginusername());
		List<TsysUser> list=sysUserService.selectByExample(tsysUserExample);
		TsysUser user=list.get(0);
		user.setIsforbidden(1);
		tsysUserMapper.updateByPrimaryKeySelective(user);
		record.setYuliu1("1");
	//前台用户表里禁用商家
	TAbbMember tAbbMember=new TAbbMember();
	tAbbMember.setId(oldrecord.getUserid());
	tAbbMember.setYuliu3("1");
	tAbbMemberMapper.updateByPrimaryKeySelective(tAbbMember);
	//把该商户的所有商品禁用掉
	tAbbGoodsMapper.updateForbiddenByShopid(1,record.getId());
	return tAbbRenzhengMapper.updateByPrimaryKeySelective(record);
	}
//启用
	@Transactional
	public int qyedit(TAbbRenzheng record) {
		TAbbRenzheng oldrecord=tAbbRenzhengMapper.selectByPrimaryKey(record.getId());
		//更新user表里的的启用状态
		TsysUserExample tsysUserExample=new TsysUserExample();
		tsysUserExample.createCriteria().andUsernameEqualTo(oldrecord.getLoginusername());
		List<TsysUser> list=sysUserService.selectByExample(tsysUserExample);
		TsysUser user=list.get(0);
		user.setIsforbidden(0);
		tsysUserMapper.updateByPrimaryKeySelective(user);
		record.setYuliu1("0");

		//前台用户表里启用商家
		TAbbMember tAbbMember=new TAbbMember();
		tAbbMember.setId(oldrecord.getUserid());
		tAbbMember.setYuliu3("0");
		tAbbMemberMapper.updateByPrimaryKeySelective(tAbbMember);
		//启用该商户的所有商品
		tAbbGoodsMapper.updateForbiddenByShopid(0,record.getId());
		return tAbbRenzhengMapper.updateByPrimaryKeySelective(record);
	}


	public TAbbRenzheng getRenzhengStatus(String userid){
		TAbbRenzheng renzheng = tAbbRenzhengMapper.getRenzhengStatus(userid);
		return renzheng;
	}

}
